﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Pan
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToInt32(Master.uid) > 0)
            {
                Response.Redirect("/disk.aspx");
            }
        }

        protected void RegBtn_Click(object sender, EventArgs e)
        {
            if (Password.Text != Confirm.Text)
            {
                Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.msg('两次密码输入不一致')})</script>";
                return;
            }
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PanConnectionString"].ToString();
            Master.RespLabel.Text = "";
            try
            {
                conn.Open();
                // 检查用户名占用
                SqlCommand cmdTest = new SqlCommand();
                cmdTest.Connection = conn;
                cmdTest.CommandType = CommandType.StoredProcedure;
                cmdTest.CommandText = "Login";
                cmdTest.Parameters.Add("@name", SqlDbType.VarChar, 255).Value = Username.Text.Trim();
                SqlDataReader sdr = cmdTest.ExecuteReader();
                bool registered = sdr.Read();
                sdr.Close();
                if (registered)
                {
                    Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.msg('用户名已被注册')})</script>";
                }
                else
                {
                    // 注册
                    SqlCommand cmdReg = new SqlCommand();
                    cmdReg.Connection = conn;
                    cmdReg.CommandType = CommandType.StoredProcedure;
                    cmdReg.CommandText = "Register";
                    cmdReg.Parameters.Add("@name", SqlDbType.VarChar, 255).Value = Username.Text.Trim();
                    cmdReg.Parameters.Add("@pwd", SqlDbType.VarChar, 255).Value = Password.Text.Trim();
                    if (cmdReg.ExecuteNonQuery() > 0)
                    {
                        Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.alert('注册成功',function(){document.location.href='/login.aspx'})})</script>";
                    }
                    else
                    {
                        Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.alert('注册失败，请重试')})</script>";
                    }
                }
                
            }
            catch(Exception err)
            {
                Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.alert('" + err.Message + "')})</script>";
            }
            finally
            {
                conn.Close();
            }
        }
    }
}